package ci092.trabalho.web;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import ci092.trabalho.DadosInscricao;
import ci092.trabalho.Professor;

import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RooWebScaffold(path = "dadosinscricaos", formBackingObject = DadosInscricao.class)
@RequestMapping("/dadosinscricaos")
@Controller
public class DadosInscricaoController {
	
	@RequestMapping(method = RequestMethod.POST)
    public String create(@Valid DadosInscricao dadosInscricao, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        if (bindingResult.hasErrors()) {
            uiModel.addAttribute("dadosInscricao", dadosInscricao);
            return "dadosinscricaos/create";
        }
        uiModel.asMap().clear();
        dadosInscricao.persist();
        return "redirect:/dadosinscricaos/" + encodeUrlPathSegment(dadosInscricao.getId().toString(), httpServletRequest);
    }
	
	  @RequestMapping(method = RequestMethod.PUT)
	    public String update(@Valid DadosInscricao dadosInscricao, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
	        if (bindingResult.hasErrors()) {
	            uiModel.addAttribute("dadosInscricao", dadosInscricao);
	            return "dadosinscricaos/update";
	        }
	        uiModel.asMap().clear();
	        dadosInscricao.merge();
	        Professor professor = new  Professor();
	        professor.setCandidato((dadosInscricao.getCandidato()));
	        professor.persist();
	        
	        uiModel.addAttribute("professor", professor);
	    	return "professors/update";
	      
	    }
	    
}
